<template>
  <DataTable searchPlaceholder="请输入医生相关信息" :columns="columns" dialogTitle="医生信息表" :tableData="recordData"
    :formFields="formFields" apiUrl="record" :delUser="delUser" />
</template>

<script setup>
import DataTable from '../../components/DataTable.vue';
import { ref, onMounted } from "vue";
import axios from "axios";

// 获取当前用户id
const storedUser = JSON.parse(localStorage.getItem('loggedInUser'))
console.log("record", storedUser)

// 定义表格列配置
const columns = [
  { prop: 'patientsName', label: '预约人', width: '130' },
  { prop: 'doctorName', label: '预约医生', width: '130' },
  { prop: 'makeDate', label: '预约时间', width: 'show-overflow-tooltip' },
  { prop: 'makeState', label: '是否叫号', width: '200' },
];

// 定义表单字段配置
const formFields = [
  { label: '预约人', model: 'patientsName' },
  { label: '预约医生', model: 'doctorName' },
  { label: '预约时间', model: 'makeDate' },
  { label: '是否叫号', model: 'makeState' }
];

// 定义医生数据
const recordData = ref([]);

// 获取所有信息
async function getUsers() {
  try {
    const response = await axios.get("/api/make/getAll");

    if (storedUser.role == 0) {
      recordData.value = response.data.data
        .filter(item => item.id === storedUser.id) // 过滤条件：只保留id匹配的记录
        .map(item => ({
          ...item,
          makeState: item.makeState === 1 ? '已叫号' : '未叫号'
        }));

    } else {
      recordData.value = response.data.data
        .map(item => ({
          ...item,
          makeState: item.makeState === 1 ? '已叫号' : '未叫号'
        }));

    }

    console.log("recordData:", recordData.value);
  } catch (error) {
    console.error("获取医生数据时出错:", error);
  }
}

// 删除数据确
async function delUser(index, row) {
  try {
    await axios.delete(`api/make/delMake/${row.id}`);
    console.log("管理员信息删除：", row.id)
    location.reload()
  } catch (error) {
    console.error("删除用户时出错:", error);
  }
}

// 在组件挂载时获取医生数据
onMounted(() => {
  getUsers()
});
</script>
